package cn.kgc.crowd1048.fundingadmin.service.impl;

import cn.kgc.crowd1048.fundingadmin.entity.TAdmin;
import cn.kgc.crowd1048.fundingadmin.mapper.TAdminMapper;
import cn.kgc.crowd1048.fundingadmin.service.TAdminService;
import cn.kgc.crowd1048.fundingadmin.util.CrowdFundingUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj
 * @since 2024-04-17
 */
@Service
public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements TAdminService {

    @Override
    public TAdmin login(String loginAcct, String userPswd) {
//        定义格式
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("login_acct",loginAcct);
//        查询，把定义好的格式，通过这个查询
        List<TAdmin> list = list(queryWrapper);
//        调用自己定义的类，查看集合是否为空
        if (CrowdFundingUtils.collectionEffective(list)==false){
            return  null;
        }
//        把查询到的集合赋值给admin
        TAdmin admin = list.get(0);
        if (admin==null){
            return null;
        }
//        调用自己定义的类，把密码转化成md5格式
        String md5=CrowdFundingUtils.md5(userPswd);
//        比较，就相当于  md5.equals(admin.getUserPswd())
        if (Objects.equals(md5,admin.getUserPswd())){
            return  admin;
        }
       return null;
    }

    @Override
    public PageInfo<TAdmin> queryForkeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<TAdmin> pageInfo = null;
        QueryWrapper<TAdmin> queryWrapper = new QueryWrapper<>();
        if (keyword!=null && keyword.length()>0){
            queryWrapper.like("login_acct",keyword).or().like("user_name",keyword).or().like("email",keyword);


        }
        List<TAdmin> list = list(queryWrapper);
        pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
//删除的方法
    public void batchRemove(List<Integer> adminIdList) {
//        tAdminMapper.deleteBatchIds(adminIdList);
//        (adminIdList);
        boolean b = removeByIds(adminIdList);
    }

//    添加的方法
    @Override
    public void saveAdmin(TAdmin tAdmin) {
        boolean save = save(tAdmin);
    }

    @Override
    public TAdmin getAdminById(Integer adminId) {
        TAdmin admin = getById(adminId);
        return admin;
    }

    @Override
    public void updateAdmin(TAdmin tAdmin) {
updateById(tAdmin);

    }


}
